package cn.lingyangwl.agile.system.manager;

import cn.lingyangwl.agile.system.mapper.ArticleGroupMapper;
import cn.lingyangwl.agile.system.model.entity.ArticleGroup;
import cn.lingyangwl.framework.tool.core.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 文章组 仓储层
 *
 * @author shenguangyang
 */
@Slf4j
@Repository
public class ArticleGroupManager extends ServiceImpl<ArticleGroupMapper, ArticleGroup>
    implements IService<ArticleGroup> {

    /**
     * 添加文章数量
     * @param groupId 组id
     */
    public void addArticleNum(Long groupId) {
        if (Objects.isNull(groupId)) {
            return;
        }
        LambdaUpdateWrapper<ArticleGroup> luw = ArticleGroup.luw().eq(ArticleGroup::getId, groupId)
                .setSql("article_num = article_num + 1");
        this.update(luw);

    }

    /**
     * 减少文章数量
     * @param groupIds 组id
     */
    public void batchDecreaseArticleNum(List<Long> groupIds) {
        if (CollectionUtils.isEmpty(groupIds)) {
            return;
        }
        LambdaUpdateWrapper<ArticleGroup> luw = ArticleGroup.luw().in(ArticleGroup::getId, groupIds)
                .gt(ArticleGroup::getArticleNum, 0)
                .setSql("article_num = article_num - 1");
        this.update(luw);
    }

    public void decreaseArticleNum(Long groupId) {
        if (Objects.isNull(groupId)) {
            return;
        }
        batchDecreaseArticleNum(Collections.singletonList(groupId));
    }
}
